package view;

import java.util.ArrayList;

import javax.swing.JFileChooser;

import model.LessonGuide;
import eventHandling.Callback;
import eventHandling.EventBus;
import eventHandling.EventTyp;

public class MainFrameController {

	private LessonGuide lessonGuide;
	private EventBus eventBus;
	private MainFrame mainFrame;
	private ArrayList<Controller> subController = new ArrayList<Controller>();

	public MainFrameController(EventBus eventBus, MainFrame mainFrame,
			LessonGuide lessonGuide) {
		this.eventBus = eventBus;
		this.mainFrame = mainFrame;
		this.lessonGuide = lessonGuide;
		registerForEvents();
		mainFrame.setVisible(true);
		mainFrame.repaint();
	}

	public LessonGuide getLessonGuide() {
		return lessonGuide;
	}

	public void setLessonGuide(LessonGuide lessonGuide) {
		this.lessonGuide = lessonGuide;
	}

	private void registerForEvents() {
		// Open
		eventBus.register(EventTyp.Type.OPEN, new Callback() {
			
			public void actionPerformed() {
				new JFileChooser().showOpenDialog(mainFrame);
			}
		});
		// Exit
		eventBus.register(EventTyp.Type.EXIT, new Callback() {
			
			public void actionPerformed() {
				System.exit(0);
			}
		});

		// new Lesson was created
		eventBus.register(EventTyp.Type.LESSON_CREATED, new Callback() {
			
			public void actionPerformed() {
				mainFrame.enableAddCardButton();
			}
		});

		// new Lesson was created
		eventBus.register(EventTyp.Type.NOT_LEARNED_CARDS_AVAILABLE,
				new Callback() {
					
					public void actionPerformed() {
						mainFrame.enableLearnUnlearnedCardsButton();
					}
				});

	}

	/**
	 * Adds a Subcontroller to the MainFrameController. A Subcontroller is a
	 * Controller which is responsible for a JDialog/JFrame (or sth like that)
	 * which can be call via the MainFrame
	 * 
	 * @param newSubController a new SubController 
	 */
	public void addSubController(Controller newSubController) {
		subController.add(newSubController);
	}

}